# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.ie.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from lxml import etree
import time
from fake_useragent import UserAgent


"""
远程调试
cmd 运行：
    chrome.exe --remote-debugging-port=9222 --user-data-dir="D:\webdriver\AutomationProfile"
添加参数
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
"""

"""
驱动地址：http://selenium-release.storage.googleapis.com/index.html
WebDriver 只能在一个页面上对元素识别与定位，对于frame/iframe 表单内嵌页面上的元素无法直接定位
需要通过switch_to.frame()方法将当前定位的主体切换为frame/iframe 表单的内嵌页面中
#切换到表单
driver.switch_to.frame("x-URS-iframe")
打开新的标签页
js = f'window.open("{url}");'
driver.execute_script(js)
-------滚动条,最下面---------
page_height = driver.execute_script('return document.documentElement.scrollHeight')
driver.execute_script(f"window.scrollBy(0, {page_height})")
===滚动到元素=====
# 滑动滚动条到某个指定的元素
div = driver.find_element_by_xpath('//div[@id="page_info"]')
js4 = "arguments[0].scrollIntoView();"
driver.execute_script(js4, div)
======ActionChains滚动======
from selenium.webdriver.common.action_chains import ActionChains
doubleclick_btn = driver.find_element_by_xpath('//div[@class="page_box"]')
ActionChains(driver).move_to_element(doubleclick_btn).perform()
------Select------
# 选中这个标签，然后使用Select创建对象
selectTag = Select(driver.find_element_by_name("jumpMenu"))
# 根据索引选择
selectTag.select_by_index(1)
# 根据值选择
selectTag.select_by_value("值")
# 根据可视的文本选择
selectTag.select_by_visible_text("显示值")
# 取消选中所有选项
selectTag.deselect_all()
------Select------
------切换窗口并关闭------
# 获取当前窗口句柄
now_handle = driver.current_window_handle
# 获取所有窗口句柄
all_handles = driver.window_handles
for handle in all_handles:
    if handle != now_handle:
        driver.switch_to.window(handle)  # 切换新打开窗口
        driver.close()
# 返回主窗口
driver.switch_to.window(now_handle)
------切换窗口并关闭------
------滚动条scrollTop=0为顶部，100000为底部------
js="var q=document.documentElement.scrollTop=100000"
------滚动条------
------返回521反爬-----
driver.get 两次.第一次获取加密cookie。第二次提交解析后的cookie
------返回521反爬-----
---------CSS 有空格------------
driver.find_element_by_css_selector('[class="btn btn-primary"]').click()
---------CSS 有空格------------
---------获取元素Selenium------
driver.find_element_by_xpath('//img[@id="ipocaptcha_CaptchaImage"]').get_attribute("src")
"""

class Chrome_Get():
    def __init__(self):
        chrome_options = Options()
        # 设置chrome不加载图片，提高速度
        # chrome_options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
        # chrome_options.add_argument('--headless') # 无头模式
        chrome_options.add_argument('--disable-gpu')
        # chrome_options.add_argument('user-agent='+UserAgent().random)  # 修改请求头，伪装浏览器型号
        # chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 设置开发模式，避免被检测
        # chrome_options.add_argument('User-Agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"')
        # driver = webdriver.Chrome(chrome_options=chrome_options) # 已经设置PATH环境变量
        # 未在PATH环境变量中设置，需要设置驱动可执行路径
        self.driver = webdriver.Chrome(executable_path='D:\\webdriver\\IEDriverServer.exe', chrome_options=chrome_options)
        self.driver.maximize_window()  # 最大化

        # html = self.driver.page_source
        # print(html)

    # 获取内容
    def get_contents(self, url):
        self.driver.get(url)
        # 设置等待
        wait = WebDriverWait(self.driver, 10)
        print(self.driver.title)
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '//a[contains(text(),"下一页")]')))

        html = etree.HTML(self.driver.page_source)
        xpath = html.xpath('//div[@class="txt-box"]')
        for x in html.xpath('//div[@class="txt-box"]'):
            title = ""
            titleUrl = ""
            titleContent = ""
            titleTime = ""
            titleFqr = ""
            for t in x.xpath('./h3/a//text()'):
                title += t
                titleUrl = x.xpath('./h3/a/@href')[0]
            for c in x.xpath('./p[@class="txt-info"]//text()'):
                titleContent += c
                titleFqr = x.xpath('./div[@class="s-p"]/a/text()')[0]
                titleTime = x.xpath('./div[@class="s-p"]/span/text()')[0]
                page = html.xpath('.//div[@class="p-fy"]/span')[0].text
                print(page)
                time.sleep(3)

                # 点击下一页
                self.driver.find_element_by_xpath('//a[contains(text(),"下一页")]').click()
                # 点击下一页
                # try:
                #     element = WebDriverWait(driver, 10).until(
                #         EC.presence_of_element_located((By.ID, "sogou_next"))).click()
                #     # self.driver.find_element_by_id('sogou_next').click()
                # except:
                #     self.driver.quit()


if __name__ == '__main__':
    chrome_get = Chrome_Get()
    url = "https://www.365-868.com/?nr=1"
    chrome_get.get_contents(url)

